<template>
  <div class="login-container">

    <!-- 登录框-->
    <div class="login-box">
      <!--头像-->
      <div class="img-box">
        <img src="../assets/logo.png">
      </div>
      <!--登录表单-->
      <el-form ref="loginFormRef" :model="loginForm" label-width="0px" :rules="loginFormRules">
        <!--用户名输入框-->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!--密码输入框-->
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!--登录按钮-->
          <el-button type="primary" @click="handleLoginButtonClick">登录</el-button>
          <!--重置按钮-->
          <el-button type="info" @click="handleResetButtonClick()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


  </div>
</template>

<script>

  import {setToken,setTokenHead,setTokenHeaderName,setUser} from '../util/auth'
  export default {
    name: "Login.vue",
    data() {
      return {
        loginForm: {
          username: 'jet',
          password: '123456'
        },
        loginFormRules: {
          username: [
            {required: true, message: "请输入用户名", trigger: "blur"},
            {min: 3, max: 10, message: "长度在3和10个字符", trigger: "blur"}
          ],
          password: [
            {required: true, message: "请输入密码", trigger: "blur"},
            {min: 6, max: 15, message: "长度在6和15个字符", trigger: "blur"}
          ]
        }
      }
    },
    methods: {
      handleResetButtonClick() {
        this.$refs.loginFormRef.resetFields();
      },

      handleLoginButtonClick() {
        this.$refs.loginFormRef.validate((valid) => {
          if (!valid) return;

          this.api.__api__Login(this.loginForm.username, this.loginForm.password)
            .then(data => {
              /** data.data:
               *  {
                    "tokenHead": "Bearer",
                    "tokenHeaderName": "Authorization",
                    "user": {
                                "roleId": "1",
                                "roleName": "admin",
                                "id": "1",
                                "username": "jet"
                            },
                    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlSWQiOiIxIiwiaXNzIjoiaXNzdWVyIiwicm9sZU5hbWUiOiJhZG1pbiIsImlkIjoiMSIsImV4cCI6MTU4NjUyMjA4NSwidXNlcm5hbWUiOiJqZXQifQ.pbAb5VKKDPKeeenSbNgLuk4uyJu7Yl2FN84CGj_QdlM"
                  },
               * */
              //将用户信息保存到vuex仓库中
              this.$store.commit('setUser',data.data.user);
              //将用户信息保存到localStorage中
              setUser(data.data.user);
              //将token相关信息保存到localStorage中
              setToken(data.data.token);
              setTokenHead(data.data.tokenHead);
              setTokenHeaderName(data.data.tokenHeaderName);
              this.$router.push("/home");

            }).catch(error =>null);
        })
      }
    }

  }
</script>

<style lang="less" scoped>
  .login-container {
    background-color: #2b4b6b;
    height: 100%;
    margin: 0px;

    .login-box {
      width: 450px;
      height: 300px;
      background-color: #fff;
      position: absolute;
      left: 50%;
      top: 50%;
      box-shadow: 0 0 10px #fff;
      transform: translate(-50%, -50%);

      .img-box {
        width: 130px;
        height: 130px;
        border: 2px solid #eee;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 10px;
        box-shadow: 0 0 10px #fff;
        border-radius: 50%;
        background-color: #fff;

        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }

      }
    }

    .el-form {
      position: absolute;
      bottom: 0px;
      padding: 0 10px;
      width: 100%;
      box-sizing: border-box;
    }

    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }


</style>
